﻿using Hidistro.Core;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System.Text;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using wnxd_feature;
using JSShop.Model;
using System.Collections.Generic;

namespace Hidistro.UI.SaleSystem.Tags
{

    public class wnxd_FeatureList : WebControl
    {

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();

            HttpRequest Request = this.Page.Request;
            if (!string.IsNullOrEmpty(Request.QueryString["CategoryId"]))
            {
                int categoryId = int.Parse(Request.QueryString["CategoryId"]);

                Hishop_Categories category = new Juse.Interface.CategoryBrowser().GetCategoryInfo(categoryId);

                if (category.ParentCategoryId != 0)
                {
                    feature wnxd = new feature();
                    List<JSShop.Model.wnxd_feature> feature = new Juse.Interface.CategoryBrowser().GetFeature(category.CategoryId.ToString());
                    string[] wnxd_feature = null;
                    if (feature.Count > 0)
                    {
                         wnxd_feature = feature[0].Value.Split(',');
                    }
                    if (wnxd_feature!=null&&wnxd_feature.Length > 0)
                    {
                        builder.AppendFormat("<div class=\"brand_story_pp_left kkun\"><p>特点</p></div><div class=\"brand_story_pp_right\"><p class=\"bnnn bnnn1\">");
                        if (string.IsNullOrEmpty(this.Page.Request.QueryString["Staticize"]))
                        {
                            string index = Request.QueryString["feature"];
                            builder.AppendFormat("<a class=\"all {1} \" href=\"{0}\">全部</a>", this.CreateUrl("feature", ""), string.IsNullOrEmpty(index) ? "kkokk" : "");
                            for (int i = 0; i < wnxd_feature.Length; i++)
                            {
                                if (!string.IsNullOrEmpty(wnxd_feature[i]))
                                {
                                    builder.AppendFormat("<a {2} href=\"{0}\">{1}</a>", this.CreateUrl("feature", i + 1 + ""), wnxd_feature[i], index == (i + 1).ToString() ? "class=\"kkokk\"" : "");
                                }
                            }
                        }
                        else
                        {
                            string index = Request.QueryString["feature"];
                            builder.AppendFormat("<a  class=\"all {1}\" href=\"{0}\">全部</a>", this.CreateStaticizeUrl(0), string.IsNullOrEmpty(index) ? "kkokk" : "");
                            for (int i = 0; i < wnxd_feature.Length; i++)
                            {
                                if (!string.IsNullOrEmpty(wnxd_feature[i]))
                                {
                                    builder.AppendFormat("<a {2} href=\"{0}\">{1}</a>", this.CreateStaticizeUrl(i + 1), wnxd_feature[i], index == (i + 1).ToString() ? "class=\"kkokk\"" : "");
                                }
                            }
                        }
                        builder.AppendFormat("</p></div>");
                    }
                }
            }
                    
            return builder.ToString();
        }
        private string CreateUrl(string paraName, string paraValue)
        {
            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.IndexOf("?") >= 0)
            {
                string oldValue = rawUrl.Substring(rawUrl.IndexOf("?") + 1);
                string[] strArray = oldValue.Split(new char[] { Convert.ToChar("&") });
                rawUrl = rawUrl.Replace(oldValue, "");
                foreach (string str3 in strArray)
                {
                    if (!str3.ToLower().StartsWith(paraName + "=") && !str3.ToLower().StartsWith("pn="))
                    {
                        rawUrl = rawUrl + str3 + "&";
                    }
                }
                return (rawUrl + paraName + "=" + Globals.UrlEncode(paraValue));
            }
            string str4 = rawUrl;
            return (str4 + "?" + paraName + "=" + Globals.UrlEncode(paraValue));
        }

        private string CreateStaticizeUrl(int pos)
        {
            int feature;
            int brand;
            int filter;
            int rank;

            int.TryParse(this.Page.Request.QueryString["feature"], out feature);
            int.TryParse(this.Page.Request.QueryString["brand"], out brand);
            int.TryParse(this.Page.Request.QueryString["filter"], out filter);
            int.TryParse(this.Page.Request.QueryString["rank"], out rank);

            int categoryId;
            int.TryParse(this.Page.Request.QueryString["categoryId"], out categoryId);

            CategoryInfo cateif = CategoryBrowser.GetCategory(categoryId);

            return Globals.GetSiteUrls().StaticizeSubCategory(cateif.CategoryId, cateif.RewriteName, (int)cateif.ParentCategoryId, cateif.ParentRewriteName, pos, brand, filter, rank, 0);

        }

    }
}